package com.geekhalo.domainevent.v3;

import com.geekhalo.domainevent.service.*;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SpringEvent implements OrderService{
    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    @Transactional
    public void createOrder(CreateOrderCommand createOrderCommand) {

        // 1. 处理订单逻辑
        Preconditions.checkArgument(createOrderCommand.isValid());

        Order order = Order.create(createOrderCommand);

        this.orderRepository.save(order);

        OrderCreatedEvent orderCreatedEvent = OrderCreatedEvent.builder()
                .order(order)
                .build();
        this.applicationEventPublisher.publishEvent(orderCreatedEvent);
    }


}
